/**
 * 自定义函数
 * */


//列表
function getIndex(){
    var formData = {
        pageStart: pages.start,
        pageLimit: pages.limit,
    };
    console.log(formData);
    $.ajax({
        type: apis.adIndex.method,
        url: apis.adIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code != 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function getHtml(d){
    var h = '', i, id;
    h += '';
    for (i = 0; i < d.length; i ++) {
        id = d[i].id;
        h += '<tr class="text-c">';
        h += '<td>' + id + '</td>';
        h += '<td>' + d[i].uid + '</td>';
        h += '<td><img src="/' + d[i].thumb + '" height="100"/></td>';
        h += '<td>' + d[i].total + '</td>';
        h += '<td>' + d[i].num + '</td>';
        h += '<td>';
        h += d[i].isrun==1 ? '关闭' : '开启';
        h += '</td>';
        h += '<td>' + d[i].addtime + '</td>';
        h += '<td>' + d[i].uptime + '</td>';
        h += '<td class="td-manage">';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_edit(\'编辑\',\'ad-edit.html\', ' + id + ');">';
        h += '编辑';
        //h += '<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += ' &nbsp; ';
        if (d[i].isrun == 1) {
            h += '<a href="javascript:;" style="color: green;" class="ml-5" onclick="setRun(' + id + ');">开启</a>';
        } else {
            h += '<a href="javascript:;" style="color: red;" onclick="setRun(' + id + ');">关闭</a>';
        }
        h += ' <br> ';
            h += '<a href="javascript:;" style="color: red;" class="ml-5" onclick="setDel(' + id + ');">删除</a>';
        h += '<input type="hidden" id="uid' + id + '" value="' + d[i].uid + '"/>';
        h += '<input type="hidden" id="thumb' + id + '" value="' + d[i].thumb + '"/>';
        h += '<input type="hidden" id="num' + id + '" value="' + d[i].total + '"/>';
        h += '<input type="hidden" id="isrun' + id + '" value="' + d[i].isrun + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    h += '';
    return h;
}


//已展示列表
function usedList(){
    var formData = {
        ad_id: adid,
        pageStart: pages.start,
        pageLimit: pages.limit,
    };
    console.log(formData);
    $.ajax({
        type: apis.adUsed.method,
        url: apis.adUsed.url,
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code != 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(usedHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function usedHtml(d){
    var h = '', i, id;
    h += '';
    for (i = 0; i < d.length; i ++) {
        id = d[i].id;
        h += '<tr class="text-c">';
        h += '<td>' + id + '</td>';
        h += '<td>';
        if (d[i].thumb == '') {
            h += '-';
        } else {
            h += '<img src="/' + d[i].thumb + '" height="100"/>';
        }
        h += '</td>';
        h += '<td>' + d[i].pub_uid + '</td>';
        h += '<td>' + d[i].plat_uid + '</td>';
        h += '<td>' + d[i].h5code + '</td>';
        h += '<td>' + d[i].addtime + '</td>';
        h += '<td>' + d[i].usetime + '</td>';
        h += '</tr>';
    }
    h += '';
    return h;
}


//编辑
function lay_add(title,url,id,w,h){
    url += '?id=' + id;
    layer_show(title,url,w,h);
}


//编辑
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    var uid = $('#uid' + id).val();
    var thumb = $('#thumb' + id).val();
    var num = $('#num' + id).val();
    var isrun = $('#isrun' + id).val();
    if (isrun == 1) {
        msgPop({msg: '启用状态不可编辑，请切换到关闭状态'}); return
    }
    url += '&uid=' + uid;
    url += '&thumb=' + escape(thumb);
    url += '&num=' + escape(num);
    layer_show(title,url,w,h);
}


//显示图片
function showImg(e){
    var h = '';
    h += '<img src="' + e.img + '"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h);
}


//详情
function getOne(){
    if (!id) { return }
    $('#uid')[0].value = uid;
    var h = '';
    if (thumb != '') {
        h = '<img src="/' + thumb + '"/>';
        h += '<input type="hidden" id="img" value="' + thumb + '"/>';
        $('#bigShow').html(h);
    }
    $('#num')[0].value = num;
}


//广告新增
function modify(){
    var formData = {
        id: id,
        uid: $('#uid').val(),
        thumb: $('#img').val(),
        total: $('#num').val(),
    };
    if (!formData.uid) {
        msgPop({msg: '用户必须的'}); return
    }
    if (!formData.thumb) {
        msgPop({msg: '图片必须的'}); return
    }
    if (!formData.total) {
        msgPop({msg: '展示次数必须的'}); return
    }
    console.log(formData);
    $.ajax({
        type: apis.adModify.method,
        url: apis.adModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                msgPop({msg: res.msg}); return
            }
            msgPop({msg: '更新成功'});
            setTimeout(function(){
                top.location.reload();
            }, 1000);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//开启|暂停
function setRun(id){
    layer.confirm('是否要切换开关状态？', {
        btn: ['确认切换', '取消'],
        time: 5000,
    }, function(index){
        console.log('确认');
        $.ajax({
            type: apis.adRun.method,
            url: apis.adRun.url,
            data: {id: id},
            success: function (res) {
                console.log(res);
                if (res.code != 0) {
                    //msgPop({msg: res.msg});
                    layerMsgErr(res.msg);
                    return
                }
                //msgPop({msg: '更新成功'});
                layerMsgOk('更新成功');
                setTimeout(function(){
                    top.location.reload();
                }, 500); return
            },
            error: function () {
                console.log('网络错误');
            }
        });
    }, function(){
        console.log('取消');
    });
}


//删除
function setDel(id){
    layer.confirm('是否要删除该记录吗？', {
        btn: ['确认删除', '取消'],
        time: 5000,
    }, function(index){
        console.log('确认');
        $.ajax({
            type: apis.adDel.method,
            url: apis.adDel.url,
            data: {id: id},
            success: function (res) {
                console.log(res);
                if (res.code != 0) {
                    //msgPop({msg: res.msg});
                    layerMsgErr(res.msg);
                    return
                }
                //msgPop({msg: '删除成功'});
                layerMsgOk('删除成功');
                setTimeout(function(){
                    top.location.reload();
                }, 500); return
            },
            error: function () {
                console.log('网络错误');
            }
        });
    }, function(){
        console.log('取消');
    });
}